<template>
  <div class="card">
    <div class="card-header">
      <h3 class="card-title">🏠 库存预警</h3>
      <a href="#" class="link">查看全部</a>
    </div>

    <!-- 滚动容器 -->
    <div class="stock-list-container">
      <div class="stock-box">
        <ul v-if="stockList.length > 0" class="stock-list">
          <li v-for="item in stockList" :key="item.name" class="stock-item">
            <div class="stock-info">
              <div class="stock-name">{{ item.name }}</div>
              <div class="stock-remark">剩余：{{ item.remain }} 件</div>
            </div>
            <span class="status" :class="getStatusClass(item.status)">
              {{ item.status }}
            </span>
          </li>
        </ul>

        <div v-else class="empty-state">
          <el-empty :image-size="60" description="暂无库存预警" />
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from "vue";

const stockList = ref([
  { name: "产品A", remain: 5, status: "低库存" },
  { name: "产品B", remain: 2, status: "紧急" },
  { name: "产品C", remain: 8, status: "低库存" },
  { name: "产品D", remain: 1, status: "紧急" },
  //   { name: "产品E", remain: 6, status: "低库存" },
  //   { name: "产品F", remain: 4, status: "低库存" },
  //   { name: "产品G", remain: 3, status: "紧急" },
  //   { name: "产品H", remain: 7, status: "低库存" },
]);
const getStatusClass = (status: string) => {
  return {
    low: status === "低库存",
    urgent: status === "紧急",
  };
};
</script>

<style lang="scss" scoped>
.card {
  background: #fff;
  border-radius: 12px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.08);
  padding: 16px;
  height: 100%;
  display: flex;
  flex-direction: column;
  box-sizing: border-box;
  border: 1px solid #e5e5e5;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 14px;
}

.card-title {
  font-size: 16px;
  font-weight: 600;
  color: #2c3e50;
  display: flex;
  align-items: center;
  gap: 6px;
}

.link {
  font-size: 13px;
  color: #409eff;
  text-decoration: none;
  font-weight: 500;
}

.link:hover {
  text-decoration: underline;
}

/* ✅ 滚动容器：现在能正确占满剩余高度 */
.stock-list-container {
  flex: 1; /* 在flex布局中占满剩余空间 */
  min-height: 0; /* ✅ 关键，允许收缩，不然会撑开父容器 */
  overflow-y: auto; /* ✅ 改成纵向滚动 */
  overflow-x: hidden;

  .stock-box {
    height: 100%;
  }
}

/* 滚动条美化 */
.stock-list-container::-webkit-scrollbar {
  width: 6px;
}

.stock-list-container::-webkit-scrollbar-thumb {
  background: #ddd;
  border-radius: 3px;
}

.stock-list-container:hover::-webkit-scrollbar-thumb {
  background: #bbb;
}

.stock-list {
  list-style: none;
  margin: 0;
  padding: 0;
}

.stock-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 0;
  border-bottom: 1px solid #f0f0f0;
  transition: background 0.2s ease;
  cursor: pointer;
}

.stock-item:hover {
  background: #f9fafb;
  border-radius: 8px;
  padding: 10px 8px;
}

.stock-info {
  flex: 1;
}

.stock-name {
  font-size: 14px;
  font-weight: 500;
  color: #333;
}

.stock-remark {
  font-size: 12px;
  color: #666;
  margin-top: 2px;
}

.status {
  padding: 4px 10px;
  border-radius: 12px;
  font-size: 12px;
  font-weight: 600;
  white-space: nowrap;
}

.status.low {
  background: #fff3cd;
  color: #856404;
  border: 1px solid #ffeaa7;
}

.status.urgent {
  background: #f8d7da;
  color: #721c24;
  border: 1px solid #f5c6cb;
}

.empty-state {
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 40px 0;
  color: #999;
}
</style>
